/*
 *
 *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
 *  * +----------------------------------------------------------------------
 *  * | Author: xw2sy@163.com
 *  * +----------------------------------------------------------------------
 *
 *  Copyright [2024] [OPENIITA]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * /
 */

package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.manager.IBizWorkorder;
import cc.iotkit.data.manager.IBizWorkorderReplace;
import cc.iotkit.manager.dto.bo.biz.BizWorkorderBo;
import cc.iotkit.manager.dto.bo.biz.BizWorkorderFailureBo;
import cc.iotkit.manager.dto.bo.biz.BizWorkorderMaintenanceBo;
import cc.iotkit.manager.dto.bo.biz.BizWorkorderReplaceBo;
import cc.iotkit.manager.dto.vo.biz.BizWorkorderVo;
import cc.iotkit.manager.service.IBizWorkorderFailureService;
import cc.iotkit.manager.service.IBizWorkorderMaintenanceService;
import cc.iotkit.manager.service.IBizWorkorderReplaceService;
import cc.iotkit.manager.service.IBizWorkorderService;
import cc.iotkit.model.biz.BizWorkorder;
import cc.iotkit.model.biz.BizWorkorderFailure;
import cc.iotkit.model.biz.BizWorkorderMaintenance;
import cc.iotkit.model.biz.BizWorkorderReplace;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 租户Service业务层处理
 *
 * @author Michelle.Chung
 */
@RequiredArgsConstructor
@Service
public class BizWorkorderServiceImpl implements IBizWorkorderService {

    private final IBizWorkorder sysBizWorkorderData;
    private final IBizWorkorderReplaceService workorderReplaceService;
    private final IBizWorkorderFailureService workorderFailureService;
    private final IBizWorkorderMaintenanceService workorderMaintenanceService;

    public BizWorkorderVo queryById(Long id) {
        return MapstructUtils.convert(sysBizWorkorderData.findById(id),BizWorkorderVo.class);
    }

    /**
     * 新增保存角色信息
     *
     * @param data 角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(BizWorkorderBo data) {
        /*if(data.getPersonId()!=null)
            data.setPersonId(LoginHelper.getUserId());*/
        BizWorkorder entity= sysBizWorkorderData.save(data.to(BizWorkorder.class));
        if(data.getType().equals("1")){
            //维保
            BizWorkorderMaintenanceBo bizWorkorderMaintenance=new BizWorkorderMaintenanceBo();
            if(data.getId()!=null)
                BeanUtils.copyProperties(workorderMaintenanceService.queryByPid(entity.getId()),bizWorkorderMaintenance);
            bizWorkorderMaintenance.setMaintenanceDate(data.getMaintenanceDate());
            bizWorkorderMaintenance.setPid(entity.getId());
            workorderMaintenanceService.save(bizWorkorderMaintenance);
        }
        else if(data.getType().equals("2")){
            //更换
            workorderReplaceService.saveAll(data.getReplaces(),entity.getId());
        }else{
            //故障
            BizWorkorderFailureBo bizWorkorderFailureBo=new BizWorkorderFailureBo();
            if(data.getId()!=null)
                BeanUtils.copyProperties(workorderFailureService.queryByPid(entity.getId()),bizWorkorderFailureBo);

            bizWorkorderFailureBo.setFailureDate(data.getFailureDate());
            bizWorkorderFailureBo.setAffect(data.getAffect());
            bizWorkorderFailureBo.setPid(entity.getId());
            workorderFailureService.save(bizWorkorderFailureBo);
        }
        return true;
    }

    @Override
    public boolean deleteById(Long id) {
        sysBizWorkorderData.deleteById(id);
        return true;
    }

    @Transactional
    public boolean deleteByIds(Collection<Long> ids) {
        sysBizWorkorderData.deleteByIds(ids);
        workorderFailureService.deleteByPid(ids);
        workorderMaintenanceService.deleteByPid(ids);
        workorderReplaceService.deleteByPid(ids);
        return true;
    }



    @Override
    public Paging<BizWorkorderVo> queryPageList(PageRequest<BizWorkorderBo> request) {
        Paging<BizWorkorder> pages=sysBizWorkorderData.findAllByConditions(request.to(BizWorkorder.class));
        return pages.to(BizWorkorderVo.class);
    }

    @Override
    public List<BizWorkorderVo> queryList(BizWorkorderBo bo) {
        List<BizWorkorder> list=sysBizWorkorderData.findAllByCondition(bo.to(BizWorkorder.class));
        return MapstructUtils.copyListToList(list,BizWorkorderVo.class);
    }



}
